home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_latex2html.idb / usr / freeware / bin / pstoimg.z / pstoimg
Text File  |  2001-01-10  |  42KB  |  1,521 lines

  1. #! /usr/freeware/bin/perl -w
  2. ##############################################################################
  3. # $Id: pstoimg.pin,v 1.11 1999/10/25 21:18:22 MRO Exp $
  4. #
  5. # pstoimg
  6. #
  7. # Accompanies LaTeX2HTML
  8. #
  9. # Script to convert an arbitrary PostScript image to a cropped GIF or PNG
  10. # image suitable for incorporation into HTML documents as inlined images
  11. # to be viewed with WWW browsers.
  12. #
  13. # This software is provided as is without any guarantee.
  14. #
  15. ##############################################################################
  16. #
  17. # $Log: pstoimg.pin,v $
  18. # Revision 1.11  1999/10/25 21:18:22  MRO
  19. #
  20. # -- added more configure options (Jens' suggestions)
  21. # -- fixed bug in regexp range reported by Achim Haertel
  22. # -- fixed old references in documentation (related to mail list/archive)
  23. #
  24. # Revision 1.10  1999/10/06 22:04:13  MRO
  25. #
  26. # -- texexpand: latex2html calls texexpand with the -out option instead of
  27. #    output redirection: this is safer on non-UNIX platforms
  28. # -- pstoimg: now there's no default cropping (useful for standalone
  29. #    conversions). latex2html was changes appropriately
  30. # -- minor cleanups in latex2html script and documentation
  31. #
  32. # Revision 1.9  1999/09/14 22:02:02  MRO
  33. #
  34. # -- numerous cleanups, no new features
  35. #
  36. # Revision 1.8  1999/07/19 09:51:00  RRM
  37. #  --  added  -aaliastext  switch, for easier way to specify anti-aliased
  38. #      font characters, without also anti-aliasing other graphics objects.
  39. #
  40. # Revision 1.7  1999/06/24 07:28:59  MRO
  41. #
  42. #
  43. # -- removed L2HMODULE
  44. # -- fixed processing of -info switch
  45. # -- changed option order for dvips on win32 (thanks JCL)
  46. # -- bumped version to 99.2a8
  47. #
  48. # Revision 1.6  1999/06/06 14:24:50  MRO
  49. #
  50. #
  51. # -- many cleanups wrt. to TeXlive
  52. # -- changed $* to /m as far as possible. $* is deprecated in perl5, all
  53. #    occurrences should be removed.
  54. #
  55. # Revision 1.5  1999/06/04 20:14:25  MRO
  56. #
  57. #
  58. # -- Reworked option parsing completely. Should behave much the same as before,
  59. #    options with -no_* work just like before.
  60. # -- Changed $NOFORK to $CAN_FORK and inverted the logic.
  61. # -- Small debugging enhancement in pstoimg
  62. #
  63. # Revision 1.4  1999/06/04 15:30:15  MRO
  64. #
  65. #
  66. # -- fixed errors introduced by cleaning up TMP*
  67. # -- made pstoimg -quiet really quiet
  68. # -- pstoimg -debug now saves intermediate result files
  69. # -- several fixes for OS/2
  70. #
  71. # Revision 1.3  1999/06/01 06:55:35  MRO
  72. #
  73. #
  74. # - fixed small bug in L2hos/*
  75. # - added some test_mode related output to latex2html
  76. # - improved documentation
  77. # - fixed small bug in pstoimg wrt. OS2
  78. #
  79. # Revision 1.2  1999/05/17 21:30:59  MRO
  80. #
  81. #
  82. # -- make texexpand warning-free and start making it use strict
  83. #    compliant
  84. #
  85. # Revision 1.1  1999/05/11 06:10:00  MRO
  86. #
  87. #
  88. # - merged config stuff, did first tries on Linux. Simple document
  89. #   passes! More test required, have to ger rid of Warnings in texexpand
  90. #
  91. # Revision 1.18  1999/05/05 19:47:03  MRO
  92. #
  93. #
  94. # - many cosmetic changes
  95. # - final backup before merge
  96. #
  97. # Revision 1.17  1999/03/15 23:00:53  MRO
  98. #
  99. #
  100. # - moved L2hos modules to top level directory, so that no dir-
  101. #   delimiter is necessary in the @INC-statement.
  102. # - changed strategy for "shave": Do not rely on STDERR redirection any
  103. #   more (caused problems on at least Win32)
  104. #
  105. # Revision 1.16  1999/02/14 23:44:34  MRO
  106. #
  107. #
  108. # -- first attempt to fix Win32 problems
  109. #
  110. # Revision 1.15  1999/02/11 00:18:29  MRO
  111. #
  112. #
  113. # -- cleaned up warppers, TeXlive stuff and Makefile
  114. #
  115. # Revision 1.14  1999/02/10 01:37:12  MRO
  116. #
  117. #
  118. # -- changed os-dependency structure again - now neat OO modules are
  119. #    used: portable, extensible, neat!
  120. # -- some minor cleanups and bugfixes
  121. #
  122. # Revision 1.13  1998/12/07 23:19:58  MRO
  123. #
  124. #
  125. # -- added POD documentation to pstoimg and did a general cleanup
  126. # -- some finetuning of config procedure and modules
  127. #
  128. # Revision 1.12  1998/10/31 14:13:05  MRO
  129. # -- changed OS-dependent module loading strategy: Modules are now located in
  130. #    different (OS-specific) directories nut have the same name: Easier to
  131. #    maintain and cleaner code
  132. # -- Cleaned up config procedure
  133. # -- Extended makefile functionality
  134. #
  135. # Revision 1.11  1998/08/09 20:45:20  MRO
  136. # -- some cleanup
  137. #
  138. # Revision 1.10  1998/06/14 14:10:38  latex2html
  139. # -- Started to implement TeXlive configuration and better OS specific
  140. #    handling (Batch files)                              (Marek)
  141. #
  142. # Revision 1.9  1998/06/07 22:35:24  latex2html
  143. # -- included things I learned from the Win95 port to config procedure:
  144. #    GS_LIB, Win32 module calls, directory separator stuff, ... (Marek)
  145. #
  146. # Revision 1.8  1998/06/01 12:57:56  latex2html
  147. # -- Cleanup and cosmetics.
  148. #
  149. # Revision 1.7  1998/05/14 22:27:37  latex2html
  150. # -- more work on config procedure (Makefile, GS_LIB)
  151. # -- tested pstoimg in 98.1 environment successfully on Linux
  152. #
  153. # Revision 1.6  1998/05/06 22:31:09  latex2html
  154. # -- Enhancements to the config procedure: Added a "generic" target
  155. #    in the Makefile for the TeXlive CD (not perfect yet)
  156. # -- included test for kpsewhich / Web2C
  157. # -- included latest stuff from Override.pm into os_*.pm
  158. #
  159. # Revision 1.5  1998/04/28 22:18:11  latex2html
  160. # - The platform specific stuff is now kept in a separate perl module. This
  161. #   does not introduce significant overhead and enhances maintainability.
  162. #
  163. # Revision 1.4  1998/03/19 23:38:06  latex2html
  164. # -- made pstoimg plug-in compatible with old one (touchwood!)
  165. # -- cleaned up, added some comments
  166. # -- inserted version information output
  167. # -- incorporated patches to make OS/2 run better (thanks Uli)
  168. # -- updated Makefile: make, make test, make install should work now
  169. #
  170. # Revision 1.3  1998/03/11 23:44:00  latex2html
  171. # -- cleaned up config.pl and reworked dvips checks
  172. # -- got pstoimg.pin up to par with the regular pstoimg
  173. # -- cosmetic changes
  174. # -- runs now under Win95 with Fabrice Popineau's Win32 tools (gs, TeX,...)
  175. #
  176. # Revision 1.2  1998/03/02 23:38:40  latex2html
  177. # Reworked configuration procedure substantially. Fixed some killing bugs.
  178. # Should now run on Win32, too.
  179. # The file prefs.pm contains user-configurable stuff for DOS platforms.
  180. # UNIX users can override the settings with the configure utility (preferred).
  181. #
  182. # Revision 1.1  1998/02/14 19:31:55  latex2html
  183. # Preliminary checkin of configuration procedure
  184. #
  185. # (end CVS log)
  186. ###############################################################################
  187.  
  188. # This file has been automatically generated by build.pl from pstoimg.pin
  189. # Do not edit this file as your changes will be lost when reconfiguring.
  190. # If you want to supply patches, please apply them to pstoimg.pin and send
  191. # the diff relative to the original pstoimg.pin. Thank you.
  192.  
  193. =head1 NAME
  194.  
  195. pstoimg - Convert a PostScript file to a bitmap image using 
  196. Ghostscript and the Netpbm utilities
  197.  
  198. =cut
  199.  
  200. use 5.003;
  201. use strict;
  202. #use diagnostics;
  203. use vars qw(*SAVEERR $LATEX2HTMLDIR $SCRIPT);
  204.  
  205. # This variable points to the DIRECTORY where the latex2html files
  206. # can be found.
  207.  
  208. use Getopt::Long;
  209.  
  210.  
  211. # see below for a description of the environment
  212.  
  213. BEGIN {
  214.   # print "scanning for l2hdir\n";
  215.   if($ENV{LATEX2HTMLDIR}) {
  216.     $LATEX2HTMLDIR = $ENV{LATEX2HTMLDIR};
  217.   } else {
  218.     $ENV{LATEX2HTMLDIR} = $LATEX2HTMLDIR = '/usr/freeware/lib/latex2html';
  219.   }
  220.  
  221.   if(-d $LATEX2HTMLDIR) {
  222.     push(@INC,$LATEX2HTMLDIR);
  223.   } else {
  224.     die qq{Fatal: Directory "$LATEX2HTMLDIR" does not exist.\n};
  225.   }
  226. }
  227.  
  228. use L2hos; # load OS-specific stuff
  229.  
  230. my $RELEASE = '99.2beta8';
  231. my ($VERSION) = q$Revision: 1.11 $ =~ /:\s*(\S+)/;
  232.  
  233. $| = 1; # unbuffer STDOUT
  234.  
  235. my $dd = L2hos->dd; # Directory delimiter
  236. my $prompt;
  237. ($prompt = $0) =~ s|^.*[/\\]||;
  238.  
  239. # Configuration as determined by "configure"
  240. #
  241. # Ghostscript
  242. my $GS = '/usr/freeware/bin/gs';
  243. my $GSDEVICE = 'pnmraw';
  244. my $GSALIASDEVICE = 'ppmraw';
  245. # Supported format(s)
  246. my @IMAGE_TYPES = qw(png gif);
  247. # Netpbm
  248. my $PNMCROP = '/usr/freeware/bin/pnmcrop';
  249. my $PPMQUANT = '/usr/freeware/bin/ppmquant';
  250. my $PNMFLIP = '/usr/freeware/bin/pnmflip';
  251. my $PNMCAT = '/usr/freeware/bin/pnmcat';
  252. my $PNMFILE = '/usr/freeware/bin/pnmfile';
  253. my $PBMMAKE = '/usr/freeware/bin/pbmmake';
  254. # GIF support
  255. my $PPMTOGIF = '/usr/freeware/bin/ppmtogif';
  256. # PNG support
  257. my $PNMTOPNG = '/usr/freeware/bin/pnmtopng';
  258. # Temporary diskspace
  259. my $def_tmp = '/tmp'; # Space for temporary files
  260.  
  261. # Some lengths used by dvips
  262. # MRO: Is this true for all runs of dvips?
  263.  
  264. my $PAGE_HEIGHT = 841.889;   # dvips page height, in pts.
  265. my $PAGE_WIDTH  = 595.275;   # dvips page width, in pts.
  266. my $PAGE_HMARGIN = 72;       # dvips margin: 1 inch = 72pt
  267. my $PAGE_VMARGIN = 72;       # dvips margin: 1 inch = 72pt
  268.  
  269. # The color to be made transparent
  270. my $trans_color = '#ffffff';
  271.  
  272. ###############################################################################
  273. # Default settings
  274. # Environment overrides defaults, command line options override everything
  275.  
  276. unless(@ARGV) {
  277.   print_help();
  278.   exit 0;
  279.   }
  280.  
  281. =head1 SYNOPSIS
  282.  
  283. B<pstoimg> B<-help> | B<-version>
  284.  
  285. B<pstoimg>
  286. S<[ B<-antialias> ]>
  287. S<[ B<-aaliastext> ]>
  288. S<[ B<-center> I<num> ]>
  289. S<[ B<-color> I<num> ]>
  290. S<[ B<-crop> I<code> ]>
  291. S<[ B<-debug> ]>
  292. S<[ B<-density> I<num>]>
  293. S<[ B<-depth> I<num> ]>
  294. S<[ B<-discard> ]>
  295. S<[ B<-flip> I<code> ]>
  296. S<[ B<-geometry> I<X>xI<Y> ]>
  297. S<[ B<-interlaced> ]>
  298. S<[ B<-margins> I<X>,I<Y> ]>
  299. S<[ B<-multipage> ]>
  300. S<[ B<-out> I<file> ]>
  301. S<[ B<-quiet> ]>
  302. S<[ B<-rightjustify> I<num> ]>
  303. S<[ B<-scale> I<num> ]>
  304. S<[ B<-tmp> I<path> ]>
  305. S<[ B<-topjustify> [B<x>]I<num> ]>
  306. S<[ B<-transparent> ]>
  307. S<[ B<-type> I<type> ]>
  308. S<[ B<-shoreup> I<num>[B<d>] ]>
  309. S<[ B<-white> ]>
  310. I<file>
  311. S<[ I<file2> ... ]>
  312.  
  313. =cut
  314.  
  315. my %opt = ();
  316. unless(&GetOptions(\%opt, qw(-help -version -debug -discard -antialias -aaliastext
  317.   -multipage -type=s -gif -png -out=s -depth=i -color=i -flip=s -density=i
  318.   -scale=f -geometry=s -margins=s -crop=s -transparent -interlaced 
  319.   -rightjustify=i -center=i -topjustify=s -shoreup=s -tmp=s -white -quiet))) {
  320.   print_usage("$prompt: Error: Invalid option(s) specified.");
  321.   exit 1;
  322. }
  323.  
  324. =head1 OPTIONS
  325.  
  326. The command line options may be abbreviated to the shortest unique
  327. prefix.
  328.  
  329. =over 4
  330.  
  331. =item B<-help>
  332.  
  333. Show this help page and exit.
  334.  
  335. =cut
  336.  
  337. if($opt{help}) {
  338.   print_help();
  339.   exit 0;
  340.   }
  341.  
  342. =item B<-version>
  343.  
  344. Show the release and version of pstoimg and exit.
  345.  
  346. =cut
  347.  
  348. if($opt{version}) {
  349.   print_version();
  350.   exit 0;
  351.   }
  352.  
  353. banner() unless($opt{quiet});
  354.  
  355. =item B<-antialias>
  356.  
  357. Use Ghostscript's anti-aliasing feature for rendering "softer" images.
  358. This applies to lines and edges of polygonal and oval or circular shapes.
  359. Only valid if Ghostscipt 4.03 or higher is installed.
  360.  
  361. =item B<-aaliastext>
  362.  
  363. Use Ghostscript's anti-aliasing feature for "smoother" font characters,
  364. without the jagged edges. Similar to B<-antialias> for graphic components.
  365. Only valid if Ghostscipt 4.03 or higher is installed.
  366.  
  367. =item B<-center> I<num>
  368.  
  369. Add the appropriate amount of whitespace to the left of the image so
  370. that the image appears to be centered in a total width of I<num> pixels.
  371.  
  372. =cut
  373.  
  374. my $CENTER = 0; # No centering by default
  375. if($opt{center}) {
  376.   $CENTER = $opt{center};
  377.   die <<"EOF" unless ($CENTER =~ /^\d+$/ && $CENTER > 0);
  378. $prompt: Error: Illegal width for -center specified: "$CENTER"
  379.          Value must be a positive integer.
  380. EOF
  381.   }
  382.  
  383. =item B<-crop> I<code>
  384.  
  385. Crop the bitmap from the given directions. I<code> may be a string of
  386. several cropping instructions, which are executed strictly in the given
  387. order. Possible values are: B<h> (horizontal, i.e. crop top and
  388. bottom), B<v> (vertical), B<tblr> (top, bottom, left, right) and B<a> (all
  389. directions). A special case is B<s>: "shave" the image at the bottom, but only
  390. if a single line of whitespace exists.
  391.  
  392. =cut
  393.  
  394. my $EXTRA_CROP = '';
  395. if($opt{crop}) {
  396.   $EXTRA_CROP = lc($opt{crop});
  397.   die <<"EOF" unless ( $EXTRA_CROP =~ /^([vhtblras]+)$/i );
  398. $prompt: Error: Illegal crop specified: "$EXTRA_CROP"
  399.          Crop must be  h, v, t, b, l, r, a, s  or combination
  400. EOF
  401.   }
  402.  
  403. =item B<-debug>
  404.  
  405. Turn on debugging output. This can get rather verbose. Any intermediate
  406. files generated are not removed to help debugging.
  407.  
  408. =cut
  409.  
  410. if($ENV{DEBUG}) {
  411.   $opt{debug} = 1;
  412.   }
  413.  
  414. =item B<-density> I<num>
  415.  
  416. The density (resolution) in DPI in which to render the bitmap. The default
  417. is 72.
  418.  
  419. =cut
  420.  
  421. my $DENSITY = 72;
  422. if($opt{density}) {
  423.   $DENSITY = $opt{density};
  424.   }
  425. elsif($ENV{DENSITY}) {
  426.   $DENSITY = $ENV{DENSITY};
  427.   }
  428. die <<"EOF" unless $DENSITY =~ /^\d+$/;
  429. $prompt: Error: Illegal density specified: "$DENSITY"
  430.          Density must be an integer value. Default is 72.
  431. EOF
  432.  
  433. =item B<-depth> I<num> or B<-color> I<num>
  434.  
  435. Specify the color depth of the bitmap. Legal values are 1 (black & white),
  436. 8 (256 colors) and 24 (true color).
  437.  
  438. =cut
  439.  
  440. unless($opt{depth}) {
  441.   if($opt{color}) {
  442.     $opt{depth} = $opt{color};
  443.     }
  444.   elsif($ENV{DEPTH}) {
  445.     $opt{depth} = $ENV{DEPTH};
  446.     }
  447.   else {
  448.     $opt{depth} = 8;
  449.     }
  450.   }
  451. die <<"EOF" unless $opt{depth} =~ /^(1|8|24)$/;
  452. $prompt:  Error: Illegal color depth specified: "$opt{depth}"
  453.           Depth must be either 1, 8 or 24.
  454. EOF
  455.  
  456. =item B<-discard>
  457.  
  458. Delete the input postscript file if the conversion was successful. Setting
  459. the environment DISCARD to a true value (as perl sees it) has the same
  460. effect.
  461.  
  462. =cut
  463.  
  464. if($ENV{DISCARD}) {
  465.   $opt{discard} = 1;
  466.   }
  467.  
  468. =item B<-flip> I<code>
  469.  
  470. Flip all generated output bitmaps. The following codes are recognized:
  471. lr (flip left-right), tb (flip top-bottom), xy (flip bottom/left-top/right),
  472. r90 and ccw (rotate by 90 degrees counterclockwise), r270 and cw (rotate
  473. 90 degrees clockwise) and r180 (rotate 180 degrees).
  474.  
  475. =cut
  476.  
  477. if($opt{flip}) {
  478.   $opt{flip} = lc($opt{flip});
  479.   die <<"EOF" unless $opt{flip} =~ /^(lr|tb|xy|r90|ccw|r270|cw|r180)$/;
  480. $prompt: Error: Illegal flip option specified: "$opt{flip}"
  481.          Flip must be one of: lr tb xy r90 ccw r270 cw r180
  482. EOF
  483.   }
  484.  
  485. =item B<-geometry> I<X>xI<Y>
  486.  
  487. Render only this "window" of the PostScript file. If given, this option
  488. can dramatically reduce memory requirements and speed up conversion. The
  489. geometry is automatically detected in case of EPS files (Encapsulated
  490. PostScript).
  491.  
  492. =cut
  493.  
  494. my $GEOMETRY = '';
  495. if($opt{geometry}) {
  496.   $GEOMETRY = $opt{geometry};
  497.   if($GEOMETRY =~ s/-//o ) {
  498.     $EXTRA_CROP .= 'bl';
  499.     }
  500.   die <<"EOF" unless ($GEOMETRY =~ /^\d+x\d+$/i);
  501. $prompt: Error: Illegal geometry specified: "$GEOMETRY"
  502.          Geometry must be <width>x<height>
  503. EOF
  504.   }
  505.  
  506. =item B<-interlaced>
  507.  
  508. Generate an interlaced bitmap. Interlaced images build up from coarse to
  509. fine as they are loaded. This option may not work on every installation
  510. and/or bitmap type, depending of the capabilities of external programs.
  511.  
  512. =cut
  513.  
  514. my $INTERLACE = 0; # Do not make interlaced images by default
  515. if($opt{interlaced}) {
  516.   $INTERLACE=1;
  517.   }
  518.  
  519. =item B<-margins> I<X>,I<Y>
  520.  
  521. The offset of the rectangle in the postscript file that is going to be
  522. rendered from top/left. Can be used together with B<-geometry> to further
  523. reduce the size of the intermediate bitmap file generated by Ghostscript.
  524.  
  525. =cut
  526.  
  527. if($opt{margins}) {
  528.   die <<"EOF" unless (($opt{margins} =~ /^(\d+),(\d+)$/));
  529. $prompt: Error: Illegal margins specified: "$opt{margins}"
  530.          Margins must be <hmargin>,<vmargin>
  531. EOF
  532.   $PAGE_HMARGIN = $1;
  533.   $PAGE_VMARGIN = $2;
  534.   }
  535.  
  536. =item B<-multipage>
  537.  
  538. Process a multi-page PostScript file, i.e. create an individual bitmap
  539. for every page. The resulting files are numbered: The decimal number
  540. (starting with 1) is appended to the basename of the PostScript input
  541. file (or the basename of the filename specified with B<-out>), while
  542. keeping the extension.
  543.  
  544. =item B<-out> I<file>
  545.  
  546. The file where to write the bitmap. If multiple PostScript files are
  547. supplied on the command line, this option is ignored. The bitmap type
  548. extension is appended automatically if I<file> does not contain a dot.
  549. In connection with B<-multipage> I<file> is extended by the page number
  550. as shown in this example:
  551.  
  552. -outfile foo.gif  --------E<gt> foo1.gif, foo2.gif, ...
  553.  
  554. =cut
  555.  
  556. if(!$opt{out} && $ENV{OUTFILE}) {
  557.   $opt{out} = $ENV{OUTFILE};
  558.   }
  559.  
  560. =item B<-quiet>
  561.  
  562. Do not print anything except error messages.
  563.  
  564. =item B<-rightjustify> I<num>
  565.  
  566. Add the appropriate amount of whitespace to the left of the image so that
  567. it appears to be aligned to the right in a total width of I<num> pixels.
  568.  
  569. =cut
  570.  
  571. my $RIGHT_JUSTIFY = 0; # No right justifying by default
  572. if($opt{rightjustify}) {
  573.   $RIGHT_JUSTIFY = $opt{rightjustify};
  574.   die <<"EOF" unless ($RIGHT_JUSTIFY =~ /^\d+$/ && $RIGHT_JUSTIFY > 0);
  575. $prompt: Error: Illegal width for -rightjustify specified: "$RIGHT_JUSTIFY"
  576.          Value must be a positive integer.
  577. EOF
  578.   }
  579.  
  580. =item B<-scale> I<factor>
  581.  
  582. Scale the image by I<factor>. Valid choices are any numbers greater than
  583. zero. Useful choices are numbers between 0.1 - 5.
  584. Large numbers may generate very large intermediate files and will take
  585. longer to process. If this option is omitted, the environment SCALE is
  586. considered.
  587.  
  588. =cut
  589.  
  590. unless($opt{scale}) {
  591.   if($ENV{SCALE}) {
  592.     $opt{scale} = $ENV{SCALE};
  593.     }
  594.   else {
  595.     $opt{scale} = 1;
  596.     }
  597.   }
  598. die <<"EOF" unless ($opt{scale} =~ /^[\d.e]+$/i && $opt{scale} > 0);
  599. $prompt: Error: Illegal scale specified: "$opt{scale}"
  600.          Scale must be nonnegative float value.
  601. EOF
  602.  
  603. =item B<-shoreup> I<num>[B<d>]
  604.  
  605. Make height and width of the bitmap(s) an exact multiple of I<num>. If
  606. I<num> is followed by a "d", then half the extra vertical space is placed
  607. underneath. This option is useful, if you want to have "blown-up" images
  608. of high quality for print, but downscale them in HTML using
  609. C<E<lt>IMG WIDTH=x HEIGHT=yE<gt>>. If the actual image is is not an
  610. integer multiple of x,y then browsers tend to display distorted images.
  611.  
  612. =cut
  613.  
  614. my $SHORE_UP = 0; # No pixel alignment by default
  615. if($opt{shoreup}) {
  616.   $SHORE_UP = $opt{shoreup};
  617.   die <<"EOF" unless $SHORE_UP =~ /^\d+d?$/i;
  618. $prompt: Error: Illegal shore-up specified: "$SHORE_UP"
  619.          Value must be a positive integer, optionally followed by d
  620. EOF
  621.   }
  622.  
  623. =item B<-tmp> I<path>
  624.  
  625. Use I<path> to store temporary files. Defaults to /tmp on this
  626. installation. This parameter can be set by the environment B<TMP> or 
  627. B<TEMP>, too.
  628.  
  629. =cut
  630.  
  631. my $TMP = '';
  632. if($opt{tmp}) {
  633.   $opt{tmp} =~ s|\Q$dd\E+$||; # remove trailing directory separator(s)
  634.   if(-d $opt{tmp} && -r _ && -w _) {
  635.     $TMP = $opt{tmp};
  636.   } else {
  637.     print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
  638.   }
  639. }
  640. if(!$TMP && ($ENV{TMP} || $ENV{TEMP})) {
  641.   ($opt{tmp} = $ENV{TMP} || $ENV{TEMP}) =~ s|\Q$dd\E+$||;
  642.   if(-d $opt{tmp} && -r _ && -w _) {
  643.     $TMP = $opt{tmp};
  644.   } else {
  645.     print "$prompt: Warning: Cannot use $opt{tmp} as temporary directory.\n";
  646.   }
  647. }
  648. if(!$TMP && -d $def_tmp && -r _ && -w _) {
  649.   $TMP = $def_tmp;
  650. }
  651. print "$prompt: Temporary directory is $TMP\n" if($opt{debug});
  652.  
  653. =item B<-topjustify> [B<x>]I<num>
  654.  
  655. Add padding whitespace to the image so that it gets a defined height.
  656. If an integer value is given, it defines the total height. The whitespace
  657. is added at the bottom. If the number is preceded by "x", then this
  658. multiple of the image height is added as whitespace at the bottom.
  659.  
  660. =cut
  661.  
  662. my $TOP_JUSTIFY = 0; # No top justifying by default
  663. if($opt{topjustify}) {
  664.   $TOP_JUSTIFY = $opt{topjustify};
  665.   die <<"EOF" unless $TOP_JUSTIFY =~ /^x?\d+[.]?\d*$/i;
  666. $prompt: Error: Illegal align specified: "$TOP_JUSTIFY"
  667.          Value must be positive numeric, optionally preceded by x
  668. EOF
  669.   }
  670.  
  671. =item B<-transparent>
  672.  
  673. Generate transparent bitmaps, i.e. the background color (white) is
  674. transparent if viewed with certain viewers (e.g. browsers). This option
  675. may not be available due to missing capabilities of external
  676. programs.
  677.  
  678. =cut
  679.  
  680. my $TRANSPARENT = 0; # Do not make make images transparent by default
  681. if($opt{transparent}) {
  682.   $TRANSPARENT = 1;
  683.   }
  684.  
  685. =item B<-type> I<type>
  686.  
  687. Instruct pstoimg to render the bitmap in I<type> format. Depending on 
  688. the local installation, pstoimg is capable of generating either GIF or
  689. PNG bitmaps. This site features the following types: png gif
  690.  
  691. If omitted, the first type in this list is taken.
  692.  
  693. =cut
  694.  
  695. if($opt{type}) {
  696.   $opt{type} = lc($opt{type});
  697.   die <<"EOF" unless grep($_ eq $opt{type},@IMAGE_TYPES);
  698. $prompt: Error: This version of pstoimg does not support
  699.     "$opt{type}" image format.
  700. EOF
  701.   }
  702. else {
  703.   ($opt{type}) = @IMAGE_TYPES; # default image type
  704.   }
  705. # Support -gif and -png for a transition period
  706. if($opt{gif}) {
  707.   print qq{$prompt: Warning: The -gif switch is deprecated. Use "-type gif" instead.\n};
  708.   if(grep($_ eq 'gif',@IMAGE_TYPES)) {
  709.     $opt{type} = 'gif';
  710.     }
  711.   else {
  712.     die <<"EOF";
  713. $prompt: Error: This version of pstoimg does not support "gif" format.
  714. EOF
  715.     }
  716.   }
  717. if($opt{png}) {
  718.   print qq{$prompt: Warning: The -png switch is deprecated. Use "-type png" instead.\n};
  719.   if(grep($_ eq 'png',@IMAGE_TYPES)) {
  720.     $opt{type} = 'png';
  721.     }
  722.   else {
  723.     die <<"EOF";
  724. $prompt: Error: This version of pstoimg does not support "png" format.
  725. EOF
  726.     }
  727.   }
  728.  
  729. =item B<-white>
  730.  
  731. Remove TeX's page color information from the PostScript file before 
  732. converting so that a white background is used.
  733.  
  734. =back
  735.  
  736. =cut
  737.  
  738. # do some consistency checks on the options
  739.  
  740.  
  741. die <<"EOF" if($RIGHT_JUSTIFY && $CENTER);
  742. $prompt: Error: Conflicting options -center and -rightjustify.
  743. EOF
  744.  
  745. # now setup some parameters
  746.  
  747. # calculate dpi resolution from density and scale
  748. $DENSITY = int($opt{scale} * $DENSITY + .5) if($opt{scale} != 1);
  749.  
  750. my $reduce_color = '';
  751. if($opt{depth} == 1) {
  752.   $reduce_color = "$PPMQUANT 2";
  753.   }
  754. elsif ($opt{depth} == 8) {
  755.   $reduce_color = "$PPMQUANT 256";
  756.   }
  757.  
  758. my $gs_aalias = '';
  759. if($opt{antialias}) {
  760.   $GSDEVICE = $GSALIASDEVICE;
  761.   if($opt{depth} == 1) { 
  762.     $gs_aalias = '-dTextAlphaBits=4 ';
  763.     $reduce_color = "$PPMQUANT -floyd 256";
  764.     }
  765.   else {
  766.     $gs_aalias = '-dTextAlphaBits=4 -dGraphicsAlphaBits=4 ';
  767.     }
  768.   }
  769. elsif ($opt{aaliastext}) {
  770.   $GSDEVICE = $GSALIASDEVICE;
  771.     $gs_aalias = '-dTextAlphaBits=4 ';
  772.     $reduce_color = "$PPMQUANT -floyd 256";
  773.   }
  774. my $PAPERSIZE = $ENV{PAPERSIZE} || '';
  775. # This rx matches float values in Bounding Box expressions
  776. my $Brx = '-?\d+(?:\.\d*|)';
  777.  
  778. ##############################################################################
  779. # Main program
  780.  
  781. =head1 DESCRIPTION
  782.  
  783. B<pstoimg> iterates over the given input files and runs them through
  784. Ghostscipt. The resulting pnm (portable anymap files) are processed
  785. with different Netpbm tools (cropping, color mapping, aligning, ...)
  786. and finally converted into (currently) either GIF or PNG format. The
  787. bitmaps can now be included e.g. in WWW pages.
  788.  
  789. The PostScript file is converted as is. If a valid bounding box is
  790. found (EPS format), then only this area is converted. The image is
  791. I<not> cropped by default.
  792.  
  793. =cut
  794.  
  795. die "$prompt: Error: No input file(s) specified\n"
  796.   unless(@ARGV);
  797.  
  798. # suppress diagnostics messages if possible
  799. my $NULLFILE = '/dev/null';
  800. open(STDERR, ">$NULLFILE") unless($opt{debug});
  801.  
  802. my $exit = 0;
  803.  
  804. $opt{out} = '' if(@ARGV > 1); # disable -out if multiple ps files given
  805.  
  806. my $psfile;
  807. foreach $psfile (@ARGV) {
  808.   unless (-f $psfile) {
  809.     print qq{$prompt: Error: Cannot find file "$psfile": $!\n};
  810.     exit 1;
  811.   }
  812.   $exit += (&pstoimg($psfile) ? 0 : 1);
  813. }
  814.  
  815. =head1 RETURN VALUE
  816.  
  817. =over 4
  818.  
  819. =item 0
  820.  
  821. if everything went all right
  822.  
  823. =item x
  824.  
  825. (x != 0) something went wrong. See the message output.
  826.  
  827. =back
  828.  
  829. =cut
  830.  
  831. exit $exit ? 1 : 0;
  832.  
  833. ##############################################################################
  834. # Subroutines
  835.  
  836. sub pstoimg {
  837.   my ($psfile) = @_;
  838.  
  839.   print "$prompt: Processing $psfile\n" unless($opt{quiet});
  840.   # remove a trailing suffix the same way a shell would do it
  841.   my $base = $psfile;
  842.   $base =~ s|[.][^.$dd$dd]*$||;
  843.  
  844.   my $outfile;
  845.   if($opt{out}) {
  846.     $outfile = $opt{out};
  847.     # append the type unless -outfile has a "." in it
  848.     $outfile .= ".$opt{type}" unless($outfile =~ /[.]/);
  849.   }
  850.   else {
  851.     $outfile = "$base.$opt{type}";
  852.   }
  853.  
  854.   # Invoke Ghostscript
  855.   my $pnmdir = $TMP ? "$TMP$dd" : ".$dd";
  856.   my $pnmbase = "p$$"; # keep it short for dos
  857.   my $pnmfile = $pnmdir . 
  858.     ($opt{multipage} ? "%d_${pnmbase}.pnm" : "$pnmbase.pnm");
  859.  
  860.   ps2pnm($psfile,$pnmfile) || return 0;
  861.  
  862.   my $ok = 1;
  863.   if (-f $pnmfile) {
  864.     if(crop_scale_etc($pnmfile, $outfile)) {
  865.       L2hos->Unlink($pnmfile) unless($opt{debug});
  866.       }
  867.     else {
  868.       return 0;
  869.       }
  870.     }
  871.   elsif($opt{multipage}) {
  872.     unless(opendir(DIR,$pnmdir)) {
  873.       print qq{$prompt: Error: Could not open directory "$pnmdir": $!\n};
  874.       return 0;
  875.       }
  876.     my @list = grep(/^\d+_\w*\./,readdir(DIR));
  877.     closedir(DIR);
  878.     if(@list) {
  879.       my $i;
  880.       foreach $i (@list) {
  881.         my ($n) = $i =~ /^(\d+)_/;
  882.         my $j = $outfile;
  883.         $j =~ s|(\.[^/.]*)$|$n$1|;
  884.     if(crop_scale_etc("$pnmdir$i", $j)) {
  885.           L2hos->Unlink("$pnmdir$i") unless($opt{debug});
  886.           }
  887.         else {
  888.           $ok = 0;
  889.           }
  890.         }
  891.       }
  892.     else {
  893.       goto not_found;
  894.       }
  895.     }
  896.   else {
  897.     not_found:
  898.     print "$prompt: Error: Couldn't find pnm output of $psfile\n";
  899.     }
  900.   L2hos->Unlink($psfile) if($opt{discard} && !$opt{debug});
  901.   $ok;
  902.   }
  903.  
  904. sub ps2pnm {
  905.   my ($psfile,$pnmfile) = @_;
  906.   my $gs_size = $PAPERSIZE ? "-sPAPERSIZE=$PAPERSIZE" : '';
  907.   my $gs_density = ($DENSITY != 72) ? "-r$DENSITY" : '';
  908.  
  909.   my ($bbx, $bby, $bbw, $bbh) = (0,0,0,0);
  910.   my $max_lines = 100;
  911.   my ($epsf,$have_geometry) = (0,0);
  912.  
  913.   # Parse postscript file for information
  914.   unless(open(PS, "<$psfile")) {
  915.     print qq{$prompt: Error: Cannot read "$psfile": $!};
  916.     return 0;
  917.     }
  918.   $_ = <PS>; # read one line
  919.   if( /^%!.*EPSF/ ) {
  920.     # we're in a EPSF file
  921.     $epsf = 1;
  922.     }
  923.   if($GEOMETRY || $epsf) {
  924.     while (defined ($_ = <PS>)) {
  925.       # Look for bounding box comment
  926.       # MRO: add support of precise bounding boxes
  927.       if ($epsf && (
  928.         /^%+(?:HiRes|Exact)BoundingBox:\s+($Brx)\s+($Brx)\s+($Brx)\s+($Brx)/o ||
  929.         /^\%\%BoundingBox:\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)/)) {
  930.         $bbx = 0 - $1;    $bby = 0 - $2;
  931.         $bbw = $3 + $bbx; $bbh = $4 + $bby;
  932.         if(($bbw > 0) && ($bbh > 0)) { # we have a valid bounding box
  933.           print "$prompt: EPSF dimensions are ${bbw}x$bbh\n" if($opt{debug});
  934.           # this overrides the -geometry switch
  935.           if($DENSITY) { # scale the output
  936.             my $scale = $DENSITY / 72.0;
  937.             $bbw *= $scale;
  938.             $bbh *= $scale;
  939.           }
  940.           $bbw = int($bbw + 0.99);
  941.           $bbh = int($bbh + 0.99);
  942.           $GEOMETRY = "${bbw}x${bbh}";
  943.           $have_geometry = 1;
  944.           last;
  945.           }
  946.         }
  947.       # Look for page size information
  948.       elsif($GEOMETRY && /TeXDict\s+begin\s+(\d+)\s+(\d+)\s+/) {
  949.         $PAGE_WIDTH  = int($1 / 65536 * 72 /72.27 +.5);
  950.         $PAGE_HEIGHT = int($2 / 65536 * 72 /72.27 +.5);
  951.         print "$prompt: Page dimensions are ${PAGE_WIDTH}x$PAGE_HEIGHT\n"
  952.           if($opt{debug});
  953.         # we don't have to look further for EPSF stuff at this point
  954.         last;
  955.         }
  956.       elsif(!$GEOMETRY && (/^\%\%EndComments/ || --$max_lines == 0)) {
  957.         # abort at a certain point to avoid scanning huge ps files
  958.         last;
  959.         }
  960.       }
  961.     }
  962.   close PS;
  963.   if($GEOMETRY && !$have_geometry) { # RRM: overrides $PAPERSIZE 
  964.     # no geometry info found in the Postscript file
  965.     $bbx = $PAGE_HMARGIN;
  966.     $bby = $PAGE_HEIGHT - $PAGE_VMARGIN;
  967.     unless($GEOMETRY =~ /\s*(\d+)x(\d+)/i) {
  968.       print qq{$prompt: Illegal geometry "$GEOMETRY" specified.\n};
  969.       return 0;
  970.       }
  971.     $bbw = $1 + 10;  # allow for the side-bars
  972.     $bbh = $2;
  973.     $bby = int(-$bby + $bbh + 8); # allow small margin for error
  974.     $bbx = int(-$bbx + 5);        # allow small margin for error
  975.     if($DENSITY) {
  976.       my $scale = $DENSITY / 72.0;
  977.       $bbw = int($scale * $bbw + .99);
  978.       $bbh = int($scale * $bbh + .99);
  979.       }
  980.     $bbw += 10;  # add a 5pt margin for safety
  981.     $bbh += 40;  # add a 20pt margin for safety
  982.     $GEOMETRY = "${bbw}x$bbh";
  983.     $have_geometry = 1;
  984.     }
  985.   if($have_geometry) {
  986.     $gs_size = "-g$GEOMETRY ";
  987.     }
  988.  
  989.   my $ps_changed = 0;
  990.   if($have_geometry || $opt{white}) {
  991.     # Remove any Postscript commands concerning Papersize if -g switch is used
  992.     # thanks to  Axel Ramge for identifying the problem and for this code
  993.     local($/) = undef;
  994.     open(PS,"<$psfile");
  995.     my $ps = <PS>;
  996.     close(PS);
  997.     my $had_papersize;
  998.     if($have_geometry) {
  999.       $had_papersize = ($ps =~ s/\n%%BeginPaperSize.*?%%EndPaperSize[^\n]*\n/\n/sg);
  1000.       }
  1001.     my $had_nonwhite;
  1002.     if($opt{white}) {
  1003.       $had_nonwhite = ($ps =~ s/(\n\d+ \d+ bop gsave) \d*\.\d+ (TeXcolorgray clippath fill grestore)/$1 1 $2/s);
  1004.       }
  1005.     $ps_changed = $had_papersize || $had_nonwhite;
  1006.     if($ps_changed) {
  1007.       my $tmppsfile = $pnmfile; # was "tmpps$$.ps"
  1008.       $tmppsfile =~ s/\.[^.]*$/.ps/;
  1009.       unless(open(PS,">$tmppsfile") && (print PS $ps) && (close PS)) {
  1010.         if($had_papersize) {
  1011.           print <<"EOF";
  1012. $prompt: Warning: Could not write "$tmppsfile": $!
  1013.     "$psfile" contains %%Papersize comments.
  1014.     Any of these should be removed else GS will fail.
  1015. EOF
  1016.           }
  1017.         if($had_nonwhite) {
  1018.           print <<"EOF";
  1019. $prompt: Warning: Could not write "$tmppsfile": $!
  1020.     "$psfile" has a non-white background.
  1021.     This may cause ugly images.
  1022. EOF
  1023.           }
  1024.         }
  1025.       $psfile = $tmppsfile;
  1026.       print qq{Debug: Papersize comment in "$psfile" deleted.\n}
  1027.         if($had_papersize && $opt{debug});
  1028.       print qq{Debug: Background switched to white in "$psfile".\n}
  1029.         if($had_nonwhite && $opt{debug});
  1030.       }
  1031.     }
  1032.  
  1033.   my $gs_quiet = $opt{debug} ? '' : '-q -dNOPAUSE -dNO_PAUSE';
  1034.   my $out_redirect = $opt{debug} ? '' : "> $NULLFILE";
  1035.   my $gs_out = "-sOutputFile=$pnmfile";
  1036.   my $gsfile = $psfile;
  1037.   # Ghostscript understands only '/' as path delimiter!
  1038.   if($opt{debug}) {
  1039.     print "$prompt: Running $GS $gs_quiet -sDEVICE=$GSDEVICE $gs_size $gs_density $gs_aalias $gs_out $out_redirect\n";
  1040.     print "GS>$bbx $bby translate\n" if($have_geometry);
  1041.     print "GS>($gsfile) run\n";
  1042.     print "GS>showpage\n" if ($epsf);
  1043.     print "GS>quit\n";
  1044.     }
  1045.   open (GS, "|$GS $gs_quiet -sDEVICE=$GSDEVICE $gs_size $gs_density $gs_aalias $gs_out $out_redirect");
  1046.   print GS "$bbx $bby translate\n" if ($have_geometry);
  1047.   print GS "($gsfile) run\n";
  1048.   print GS "showpage\n" if ($epsf);
  1049.   print GS "quit\n";
  1050.   print "\n" if($opt{debug});
  1051.   unless(close(GS)) {
  1052.     print "$prompt: Error: Ghostscript returned error status ",$?>>8,"\n";
  1053.     }
  1054.   L2hos->Unlink($psfile) if($ps_changed && !$opt{debug});
  1055.   1;
  1056.   }
  1057.  
  1058.  
  1059. # This sub post-processes the PNM images that come out of Ghostscript.
  1060. # The image is cropped, flipped and finally converted to PNG or GIF.
  1061.  
  1062. sub crop_scale_etc {
  1063.   my ($in, $out) = @_;
  1064.  
  1065.   # create temp filename; should be auto-incrementable
  1066.   my $tmp = $in;
  1067.   $tmp =~ s/(\.[^.]*)?$/.t00/;
  1068.   # save the original Ghostscript result
  1069.   if($opt{debug}) {
  1070.     L2hos->Copy($in,$tmp);
  1071.     &increment_name($tmp);
  1072.   }
  1073.   my ($cmd,$type,$width,$height,$just);
  1074.  
  1075.   my $must_align = 0;
  1076.   #$EXTRA_CROP = "a$EXTRA_CROP" # hack to ensure first all-over cropping
  1077.   #  unless($EXTRA_CROP =~ /^a/i);
  1078.  
  1079.   # RRM: Remove justification bars
  1080.   $EXTRA_CROP =~ s/h/bt/gi; # crop horizontally
  1081.   $EXTRA_CROP =~ s/v/rl/gi; # crop vertically
  1082.   while ($EXTRA_CROP =~ /([atblrs])/gi) {
  1083.     my $edge = $1;
  1084.     my $croparg = '';
  1085.     if($edge =~ /b/i) {
  1086.       $croparg = '-bot ';
  1087.     } elsif($edge =~ /[tlr]/i) {
  1088.       $croparg = "-$edge ";
  1089.     } elsif($edge =~ /s/i) {
  1090.       #RRM: shave at most 1-2 rows of white from the bottom
  1091.       if($cmd) {
  1092.         # Terminate command pipe
  1093.         &do_cmd($in,$tmp,$cmd) || return 0; # failure
  1094.         $cmd = '';
  1095.       }
  1096.       my ($type,$width,$height) = get_image_geometry($in);
  1097.       next unless($type); # skip if no geometry
  1098.       if(&do_cmd_norename("$PNMCROP -bot < $in",$tmp)) {
  1099.         my ($type,$width,$height2) = get_image_geometry($tmp);
  1100.         if($type && ($height - $height2) < 3 ) {
  1101.           # command succeeded and shaved less than 3 rows
  1102.           if($opt{debug}) {
  1103.             L2hos->Copy($tmp,$in);
  1104.             &increment_name($tmp);
  1105.           } else {
  1106.             L2hos->Rename($tmp,$in);
  1107.           }
  1108.           next;
  1109.         }
  1110.       }
  1111.       # MRO: this shouldn't be necessary: L2hos->Unlink($tmp);
  1112.       next; # go to next crop argument
  1113.     } # end switch on crop codes
  1114.     if($cmd) {
  1115.       # Continue command pipe
  1116.       $cmd .= "| $PNMCROP $croparg";
  1117.     } else {
  1118.       # start new pipe
  1119.       $cmd = "$PNMCROP $croparg< $in ";
  1120.     }
  1121.   } # end cropping
  1122.  
  1123.  
  1124.   if($opt{flip}) {
  1125.     unless($cmd) {
  1126.       $cmd = "$PNMFLIP -$opt{flip} < $in";
  1127.     } else {
  1128.       $cmd .= "| $PNMFLIP -$opt{flip} ";
  1129.     }
  1130.   }
  1131.  
  1132.   if($RIGHT_JUSTIFY || $TOP_JUSTIFY || $CENTER || $SHORE_UP) {
  1133.     if($cmd) {
  1134.       # empty command pipe, we need the image's geometry
  1135.       &do_cmd($in,$tmp,$cmd);
  1136.       $cmd='';
  1137.     }
  1138.  
  1139.     # Get bitmap type and dimensions
  1140.     ($type,$width,$height) = &get_image_geometry($in);
  1141.     return 0 unless($type);
  1142.  
  1143.     my ($white_left,$white_right,$white_top,$white_bottom) = (0,0,0,0);
  1144.  
  1145.     if($RIGHT_JUSTIFY || $CENTER) {
  1146.       if($RIGHT_JUSTIFY) {
  1147.         $white_left = int($RIGHT_JUSTIFY-$width);
  1148.       } else { # CENTER
  1149.         $white_left = int(($CENTER-$width) / 2);
  1150.       }
  1151.       $white_left = 0 unless($white_left > 0);
  1152.  
  1153.       $width += $white_left;
  1154.     }
  1155.  
  1156.     if($TOP_JUSTIFY) {
  1157.       if($TOP_JUSTIFY =~ /^x([0-9.]+)/io) {
  1158.         $white_bottom = $1 * $height;
  1159.       } else {
  1160.         $white_bottom = $TOP_JUSTIFY - $height;
  1161.       }
  1162.       if($white_bottom > 0) {
  1163.         $white_bottom = int($white_bottom + 0.99); # round up
  1164.         $height += $white_bottom;
  1165.       } else {
  1166.         $white_bottom = 0;
  1167.       }
  1168.     }
  1169.  
  1170.     if($SHORE_UP =~ /(\d+)(d?)/ && $1) {
  1171.       # RRM: make height and width an exact multiple of $SHORE_UP
  1172.       my ($shoreup,$depth) = ($1,$2);
  1173.       my $extra = $height % $shoreup;
  1174.       if($depth) { # image needs depth, place half the extra space underneath
  1175.         my $bextra = int($extra/2);
  1176.         $white_bottom += $bextra;
  1177.         $white_top += $extra - $bextra;
  1178.       } else {
  1179.         $white_top += $extra;
  1180.       }
  1181.  
  1182.       $extra = $width % $shoreup;
  1183.       my $rextra = int($extra/2);
  1184.       $white_right += $rextra;
  1185.       $white_left += $extra - $rextra;
  1186.       $cmd = '';
  1187.     }
  1188.  
  1189.  
  1190.   if($white_left) {
  1191.     if($cmd) {
  1192.       &do_cmd($in,$tmp,$cmd) || return 0;
  1193.     }
  1194.     # Start new command pipe
  1195.     $cmd = "$PBMMAKE -white $white_left 1 | $PNMCAT -white -lr - $in ";
  1196.   }
  1197.  
  1198.   if($white_right) {
  1199.     if($cmd) {
  1200.       &do_cmd($in,$tmp,$cmd) || return 0;
  1201.     }
  1202.     # Start new command pipe
  1203.     $cmd = "$PBMMAKE -white $white_right 1 | $PNMCAT -white -lr $in - ";
  1204.   }
  1205.  
  1206.   if($white_top) {
  1207.     if($cmd) {
  1208.       &do_cmd($in,$tmp,$cmd) || return 0;
  1209.     }
  1210.     # Start new command pipe
  1211.     $cmd = "$PBMMAKE -white 1 $white_top | $PNMCAT -white -tb - $in ";
  1212.   }
  1213.  
  1214.   if($white_bottom) {
  1215.     if($cmd) {
  1216.       &do_cmd($in,$tmp,$cmd) || return 0;
  1217.     }
  1218.     # Start new command pipe
  1219.     $cmd = "$PBMMAKE -white 1 $white_bottom | $PNMCAT -white -tb $in - ";
  1220.     }
  1221.   } # endif must_align
  1222.  
  1223.   my $pnmtoimg;
  1224.   if($opt{type} eq 'gif') {
  1225.     $pnmtoimg = $PPMTOGIF;
  1226.     if($INTERLACE) {
  1227.       $pnmtoimg .= ' -interlace';
  1228.       }
  1229.     if($TRANSPARENT) {
  1230.       $pnmtoimg .= ' -trans ' . L2hos->quote($trans_color);
  1231.     }
  1232.   }
  1233.   if($opt{type} eq 'png') {
  1234.     $pnmtoimg = $PNMTOPNG;
  1235.     if($INTERLACE) {
  1236.       $pnmtoimg .= ' -interlace';
  1237.     }
  1238.     if($TRANSPARENT) {
  1239.       $pnmtoimg .= ' -trans ' . L2hos->quote($trans_color);
  1240.     }
  1241.   }
  1242.   unless($pnmtoimg) {
  1243.     print qq($prompt: Error: unknown image type "$opt{type}".\n);
  1244.     exit 2;
  1245.   }
  1246.  
  1247.   unless($type) {
  1248.     ($type,$width,$height) = &get_image_geometry($in);
  1249.     return 0 unless($type);
  1250.   }
  1251.   # run ppmquant only on color/gray images
  1252.   if(!$type || $type =~ /(ppm|pgm)/i) {
  1253.     if($cmd) {
  1254.       $cmd .= "| $reduce_color "
  1255.     } else {
  1256.       $cmd = "$reduce_color < $in ";
  1257.     }
  1258.   }
  1259.  
  1260.   if($cmd) {
  1261.     $cmd .= "| $pnmtoimg "
  1262.   } else {
  1263.     $cmd = "$pnmtoimg < $in ";
  1264.   }
  1265.   &do_cmd_norename($cmd,$out) || return 0;
  1266.   print qq{$prompt: Written $out\n} unless($opt{quiet});
  1267.  
  1268.   1;
  1269. }
  1270.  
  1271. sub banner {
  1272.   print "$prompt V$RELEASE (Revision $VERSION, Perl $])\n";
  1273. }
  1274.  
  1275. sub print_version {
  1276.   my $formats = join(',',@IMAGE_TYPES);
  1277.   print <<"EOM";
  1278. $prompt (Revision $VERSION, perl $]),
  1279. part of LaTeX2HTML Release V$RELEASE.
  1280.  
  1281. Supported output image format(s): $formats
  1282. EOM
  1283.   1;
  1284. }
  1285.  
  1286. sub print_help {
  1287.   L2hos->perldoc($SCRIPT);
  1288.   1;
  1289. }
  1290.  
  1291. sub print_usage {
  1292.   my $start  = 0;
  1293.   my $usage  = 'Usage: ';
  1294.   my $indent = '';
  1295.  
  1296.   print (@_, "\n") if @_;
  1297.  
  1298.   my $perldoc = '/usr/freeware/bin'.$dd."perldoc";
  1299.   my $script = $SCRIPT || $0;
  1300.   open(PIPE, "$perldoc -t $script |")
  1301.      || die "Fatal: can't open pipe: $!";
  1302.   while (<PIPE>)
  1303.   {
  1304.     if (/^\s*$/) {
  1305.       next;
  1306.     } elsif (/^SYNOPSIS/) {
  1307.       $start = 1;
  1308.     } elsif (/^\w/) {
  1309.       $start = 0;
  1310.     } elsif ($start == 1) {
  1311.       ($indent) = /^(\s*)/;
  1312.       s/^$indent/$usage/;
  1313.       $usage =~ s/./ /g;
  1314.       $start = 2;
  1315.       print $_;
  1316.     } elsif ($start == 2) {
  1317.       s/^$indent/$usage/;
  1318.       print $_;
  1319.     }
  1320.   }
  1321.   close PIPE;
  1322.   1;
  1323. }
  1324.  
  1325. sub do_cmd {
  1326.   my ($in,$tmp,$cmd) = @_;
  1327.   
  1328.   print qq{Running "$cmd > $tmp"\n} if($opt{debug});
  1329.   my $stat = system("$cmd > $tmp");
  1330.   if($stat) { # error
  1331.     print qq{$prompt: Error: "$cmd > $tmp" failed: $!\n};
  1332.     return 0; # failure
  1333.   }
  1334.   elsif(!-s $tmp) { # does not exist or zero size
  1335.     print qq{$prompt: Error: "$cmd > $tmp" produced empty file\n};
  1336.     L2hos->Unlink($tmp) if(-e $tmp);
  1337.     return 0; # failure
  1338.   }
  1339.   if($opt{debug}) {
  1340.     # increase the temporary filename by 1
  1341.     # this uses perl's magic autoincrement
  1342.     &increment_name($_[1]);
  1343.     return L2hos->Copy($tmp,$in);
  1344.   } elsif(!L2hos->Rename($tmp,$in)) {
  1345.      print qq{$prompt: Error: rename of "$tmp" to "$in" failed: $!\n};
  1346.      return 0; # failure
  1347.   }
  1348.   1;
  1349. }
  1350.  
  1351. sub do_cmd_norename {
  1352.   my ($cmd,$out) = @_;
  1353.   
  1354.   print qq{Running "$cmd > $out"\n} if($opt{debug});
  1355.   my $stat = system("$cmd > $out");
  1356.   if($stat) { # error
  1357.     print qq{$prompt: Error: "$cmd > $out" failed: $!\n};
  1358.     return 0; # failure
  1359.   }
  1360.   elsif(!-s $out) { # does not exist or zero size
  1361.     print qq{$prompt: Error: "$cmd > $out" produced empty file\n};
  1362.     L2hos->Unlink($out) if(-e $out);
  1363.     return 0; # failure
  1364.   }
  1365.   1;
  1366. }
  1367.  
  1368. sub do_cmd_plain {
  1369.   my ($cmd) = @_;
  1370.   
  1371.   print qq{Running "$cmd"\n} if($opt{debug});
  1372.   my $stat = system($cmd);
  1373.   if($stat) { # error
  1374.     print qq{$prompt: Error: "$cmd" failed: $!\n};
  1375.     return 0; # failure
  1376.   }
  1377.   1;
  1378. }
  1379.  
  1380. sub get_image_geometry {
  1381.   my ($pnmfile) = @_;
  1382.  
  1383.   my ($type,$width,$height);
  1384.   my $out = `$PNMFILE $pnmfile`;
  1385.   if($? || $out =~ /(P[BGP]M)[^0-9]*(\d+)\s*by\s*(\d+)/i) {
  1386.     $type = $1;
  1387.     $width = $2;
  1388.     $height = $3;
  1389.     print qq{Image "$pnmfile" is $type, ${width}x$height\n} if($opt{debug});
  1390.   } else {
  1391.     print "$prompt: Error: Could not determine image size: $out\n";
  1392.     return undef;
  1393.   }
  1394.   ($type,$width,$height);
  1395. }
  1396.  
  1397. # push the number in the suffix up one notch
  1398. sub increment_name {
  1399.   $_[0] =~ s/(\d+)$/$a=$1;++$a/e;
  1400. }
  1401.  
  1402. __DATA__
  1403.  
  1404. =head1 EXAMPLES
  1405.  
  1406. =over 4
  1407.  
  1408. =item C<pstoimg foo.ps>
  1409.  
  1410. Convert the first page of foo.ps to the default bitmap type.
  1411.  
  1412. =item C<pstoimg -type png -crop a -trans -interlace foo.ps>
  1413.  
  1414. Same as above, but force png output and crop all the whitespace
  1415. around the image and make the color white transparent and
  1416. generate an interlaced bitmap.
  1417.  
  1418. =item C<pstoimg -multi -out bar -type gif -crop a foo.ps>
  1419.  
  1420. Consider foo.ps a multiple page PostScript file and create output
  1421. files bar1.gif, bar2.gif, etc.
  1422.  
  1423. =back
  1424.  
  1425. =head1 ENVIRONMENT
  1426.  
  1427. =over 4
  1428.  
  1429. =item DENSITY, DEPTH, DEBUG, DISCARD
  1430.  
  1431. See B<-density>, B<-depth>, B<-debug>, B<-discard>, respectively.
  1432.  
  1433. =item GS_LIB
  1434.  
  1435. This variable is set to the path(s) where Ghostscript libraries have
  1436. been found on this system during configuration, but only if the built-in
  1437. paths are not correct. This fixes the problem of relocation that is quite
  1438. common on Win32 installations. This behavior can be overridden by
  1439. setting GS_LIB manually before starting pstoimg.
  1440.  
  1441. =item LATEX2HTMLDIR
  1442.  
  1443. The directory where the LaTeX2HTML library and perl modules are found.
  1444. Defaults to "/usr/freeware/lib/latex2html" on this installation.
  1445.  
  1446. =item OUTFILE
  1447.  
  1448. Setting this has the same effect as specifying B<-out>. Please do not rely
  1449. on this feature any more, it will disappear from the next releases!
  1450.  
  1451. =item PAPERSIZE
  1452.  
  1453. The papersize to use by Ghostscript to render the image. pstoimg tries
  1454. hard to optimize for rendering on the smallest possible bitmap size.
  1455. Still this option is there to enable tuning by hand, although it is
  1456. deprecated. If pstoimg finds a better setting, this parameter is ignored.
  1457.  
  1458. =item SCALE
  1459.  
  1460. See the discussion of B<-scale>.
  1461.  
  1462. =item TMP and TEMP
  1463.  
  1464. Unless overridden by B<-tmp>, these variables denote a directory where
  1465. to store temporary files. TMP is considered first, then TEMP.
  1466.  
  1467. =back
  1468.  
  1469. =head1 SEE ALSO
  1470.  
  1471. gs, pnmcrop, pnmquant, pbmmake, pnmcat, pnmfile, pnmflip, ppmtogif,
  1472. pnmtopng, giftool, giftrans.
  1473.  
  1474. =head1 NOTES
  1475.  
  1476. Several people have suggested to use ImageMagick's convert instead of
  1477. pstoimg. A few comments on this: convert uses (of course) Ghostscript 
  1478. for conversion of PostScript to bitmap, so one still needs gs. And
  1479. for the special requirements of LaTeX2HTML convert's features are not
  1480. sufficient. The ImageMagick toolset has everything in place, but it
  1481. has some overhead that can prove killing when processing some 100 
  1482. images. pstoimg only does what it really has to, so it should be
  1483. quite efficient. Don't get me wrong - I like ImageMagick, but not in
  1484. the context of LaTeX2HTML.
  1485.  
  1486. =head1 CAVEATS
  1487.  
  1488. This utility is automatically configured and built to work on the
  1489. local setup. If this setup changes (e.g. some of the external commands
  1490. are moved), the script has be be reconfigured.
  1491.  
  1492. Despite the portability of perl, a pstoimg configured on UNIX will
  1493. probably not work on Win32 and vice versa.
  1494.  
  1495. =head1 BUGS
  1496.  
  1497. This is a major enhancement release, so there may be a few bugs. As
  1498. the user inteface changed a bit, some of your tools that were using
  1499. pstoimg may not work any more. 
  1500.  
  1501. Please report bugs to latex2html@tug.org, stating the (debug) output
  1502. of pstoimg, your perl version and the versions of the external tools.
  1503. Best is to include the cfgcache.pm file from the configuration procedure.
  1504.  
  1505. =head1 AUTHOR
  1506.  
  1507. Marek Rouchal E<lt>marek@saftsack.fs.uni-bayreuth.deE<gt>
  1508.  
  1509. =head1 HISTORY
  1510.  
  1511. This script went through a long evolution, beginning with a modification
  1512. of Doug Crabill's E<lt>dgc@cs.purdue.eduE<gt> ps2epsi script.
  1513. The first perl version was done by Nikos Drakos <nikos@cbl.leeds.ac.uk>.
  1514. It was gradually improved by numerous LaTeX2HTML developers:
  1515. Ross Moore <ross@mpce.mq.edu.au>, Jens Lippmann 
  1516. <lippmann@rbg.informatik.tu-darmstadt.de> and others (sorry for not
  1517. mentioning everyone and thanks for your contributions).
  1518.  
  1519. =cut
  1520.  
  1521.